#include "CanvasView.h"
#include "ToolFactory.h"
QString Canvas::currentTool;
Tool* aTool;
PaintArea::PaintArea(QWidget *parent)
: QGraphicsView(parent)

{ 
QGraphicsScene scene;
setAttribute (Qt::WA_SetCursor);

currentPixmap = new QGraphicsPixmapItem;
	ourCanvas = new Canvas;
	undoPointer = -1;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

}
PaintArea::~PaintArea()
{
        for(int i = 0; i < undoVector.size();i++)
                delete undoVector[i];

}

void PaintArea::redo()
{
	int i = undoVector.size()-1;
	if(undoPointer < i)
	{
		++undoPointer;
		image = *undoVector[undoPointer];
		this->resize(image.width(), image.height());
		this->updatePixmap();
	}

}


void PaintArea::saveUndoState()
{
	undoVector.push_back(new QImage(image));
	undoPointer++;
}


void PaintArea::undo()
{
	if(undoPointer >= 0)
	{
		undoPointer--;
		image = undoPointer == -1 ? *undoVector[undoPointer+1] : *undoVector[undoPointer];
		this->updatePixmap();
		this->resize(image.width(), image.height());
		
	}


}
 void PaintArea::cleanUndoStack()
{

	for(int i = undoVector.size()-1;i > undoPointer;--i)
	{
		 delete undoVector[i];
		 undoVector.pop_back();
	}

}



QString getCurrentTool(){
	return Canvas::currentTool;
}

void PaintArea::updatePixmap()
{
	this->currentPixmap->setPixmap(QPixmap::fromImage(image));

}
void PaintArea::mouseMoveEvent(QMouseEvent* event)
{
		if ((event->buttons() == Qt::LeftButton) && scribbling) 
			aTool->mouseMoveEvent(event, this);
}

void PaintArea::setImage(QImage* sourceImage)
{

	this->image = *sourceImage;

	this->setScene(ourCanvas);
	this->currentPixmap->setPixmap(QPixmap::fromImage(*sourceImage));
	this->resize(image.width(), image.height());
	ourCanvas->addItem(currentPixmap);
	this->hide();

}

void PaintArea::mouseReleaseEvent(QMouseEvent* event)
{
	     if (event->button() == Qt::LeftButton && scribbling) {
		     aTool->mouseReleaseEvent(event, this);

			  saveUndoState();
     }


}
void PaintArea::mousePressEvent(QMouseEvent* event)
{
     if (event->button() == Qt::LeftButton && getCurrentTool() != "") 
	 {
		 
		 if(undoVector.size()-1 > undoPointer)
			cleanUndoStack();

		 aTool = ToolFactory::createTool(getCurrentTool());
		 aTool->mousePressEvent(event, this);

	 }
}




  bool PaintArea::saveImage(const QString &fileName, const char *fileFormat)
 {
	// setImage(theImage);


   /*  if (theImage.save(fileName, fileFormat)) {
         modified = false;
         return true;
     } else {
         return false;
     }*/

	 return true;
 }

       
  bool PaintArea::openImage(const QString& fileLocation)
  {
     QImage* loadedImage = new QImage;
     if (!loadedImage->load(fileLocation))
         return false;

     QSize newSize = loadedImage->size().expandedTo(size());

	 setImage(loadedImage);
	 
	 setObjectName(fileLocation);

     return true;
  }



    void PaintArea::resizeImage(QSize newSize)
 {
	//if (theImage.size() == newSize)
   //      return;

     QImage newImage(newSize, QImage::Format_RGB32);
     newImage.fill(qRgb(255, 255, 255));
     QPainter painter(&newImage);
  //   painter.drawImage(QPoint(0, 0), theImage);
//     theImage = newImage;



 }